package com.shopping.runbackground.coupons.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shopping.entity.PageResult;
import com.shopping.runbackground.coupons.api.pojo.CommonCoupons;
import com.shopping.runbackground.coupons.dao.CommomConponsDao;
import com.shopping.runbackground.coupons.service.CommonCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.shopping.entity.Page.pageSize;


@Service
public class CommonCouponsServicecImpl implements CommonCouponsService {
    //@Resource


    @Autowired
    private CommomConponsDao commomConponsDao;

    @Override
    public PageResult<CommonCoupons> findall(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CommonCoupons> page=commomConponsDao.findAll();

        // List<CommonCoupons> list=commomConponsDao.findAll();
        long total = page.getTotal();
        List<CommonCoupons> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void insert(CommonCoupons commonCoupons) {
        Date date=new Date();
        commonCoupons.setCcFtime(date);
        commomConponsDao.insert(commonCoupons);
    }

    @Override
    public void update(CommonCoupons commonCoupons) {
        commomConponsDao.update(commonCoupons);
    }

    @Override
    public void delete(Integer cc_id) {
        commomConponsDao.delete(cc_id);
    }

    @Override
    public PageResult<CommonCoupons> findbynameordate(Map map) throws ParseException {
        Integer currentPage =1;
        Integer pageSize =5;

        String ccName=null;
        String startime=null;
        String endtime=null;

        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

        if(map.containsKey("currentPage")){
            currentPage= Integer.parseInt( (String) map.get("currentPage"));
        }
        if(map.containsKey("pageSize")){
            pageSize= Integer.parseInt( (String) map.get("pageSize"));
        }

        if(map.containsKey("ccName")){
            ccName= (String) map.get("ccName");
        }
        if(map.containsKey("startTime")){
            startime=(String) map.get("startTime");
        }
        if(map.containsKey("endTime")){
            endtime= (String) map.get("endTime");
        }

        PageHelper.startPage(currentPage,pageSize);
        Page<CommonCoupons> page = commomConponsDao.findbynameordate(ccName, startime, endtime);
        long total = page.getTotal();
        List<CommonCoupons> result = page.getResult();
        return new PageResult<>(total,result);
    }
}
